package com.uuah.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.exception.AbstractUuahException;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.IUuahEvent;
import com.uuah.server.api.IUuahEventContext;
import com.uuah.server.api.IUuahMessage;
import com.uuah.server.api.component.service.IService;
import com.uuah.server.api.endpoint.IEndpointURI;
import com.uuah.server.api.endpoint.IUuahEndpoint;
import com.uuah.server.api.session.IUuahSession;
import com.uuah.server.exception.AbstractUuahServerException;
import com.uuah.server.exception.transformer.TransformerException;
import com.uuah.server.message.UuahMessage;

public class UuahEventContext implements IUuahEventContext {
	protected static final Logger logger = LoggerFactory
			.getLogger(UuahEventContext.class);

	private final IUuahEvent event;
	private final IUuahSession session;

	public UuahEventContext(IUuahEvent event) {
		this.event = event;
		this.session = event.getSession();
	}

	public IUuahMessage getMessage() {
		return event.getMessage();
	}

	public byte[] getMessageAsBytes() throws AbstractUuahException {
		return event.getMessageAsBytes();
	}

	public Object transformMessage() throws TransformerException {
		return event.transformMessage();
	}

	public Object transformMessage(Class expectedType)
			throws TransformerException {
		return event.transformMessage(expectedType);
	}

	public byte[] transformMessageToBytes() throws TransformerException {
		return event.transformMessageToBytes();
	}

	public String getMessageAsString(String encoding)
			throws AbstractUuahException {
		return event.getMessageAsString(encoding);
	}

	public String transformMessageToString() throws TransformerException {
		return event.transformMessageToString();
	}

	public String getMessageAsString() throws AbstractUuahException {
		return event.getMessageAsString();
	}

	public IUuahMessage sendEvent(Object message)
			throws AbstractUuahServerException {
		return sendEvent(new UuahMessage(message, event.getMessage()));
	}

	public IUuahMessage sendEvent(IUuahMessage message)
			throws AbstractUuahServerException {
		return session.sendEvent(message);
	}

	public void dispatchEvent(Object message)
			throws AbstractUuahServerException {
		session.dispatchEvent(new UuahMessage(message, event.getMessage()));
	}

	public void dispatchEvent(IUuahMessage message)
			throws AbstractUuahServerException {
		session.dispatchEvent(message);
	}

	public void dispatchEvent(IUuahMessage message, IEndpointURI endpointUri)
			throws AbstractUuahServerException {
		IUuahEndpoint endpoint = getUuahContext().getRegistry()
				.lookupEndpointFactory().getRequestEndpoint(endpointUri);
		session.dispatchEvent(message, endpoint);
	}

	public void dispatchEvent(IUuahMessage message, String endpointName)
			throws AbstractUuahServerException {
		session.dispatchEvent(message, endpointName);
	}

	public void dispatchEvent(IUuahMessage message, IUuahEndpoint endpoint)
			throws AbstractUuahServerException {
		session.dispatchEvent(message, endpoint);
	}

	/**
	 * �¼���Ӧ��Service
	 */
	public IService getService() {
		return event.getService();
	}

	public boolean isSynchronous() {
		return event.isSynchronous();
	}

	public IEndpointURI getEndpointURI() {
		return event.getEndpoint().getEndpointURI();
	}

	public int getTimeout() {
		return event.getTimeout();
	}

	public String getEncoding() {
		return event.getEncoding();
	}

	public IUuahSession getSession() {
		return event.getSession();
	}

	public String toString() {
		return event.toString();
	}

	public IUuahContext getUuahContext() {
		return event.getUuahContext();
	}
}
